<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Khs extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('mdl_khs');
		//$this->load->model('mdl_fakultas');
		//$this->load->model('mdl_prodi');
		//$this->output->enable_profiler(true);
	}
	
	function index(){
		$this->load->view('khs_new');
	}
	
	# grid
	function grid(){
		$data = $this->mdl_khs->getdata();
		echo $this->mdl_khs->togrid($data['row_data'], $data['row_count']);
	}
	
	function grid_detail($id){
		$data = $this->mdl_khs->getdata_detail($id);
		echo $this->mdl_khs->togrid($data['row_data'], $data['row_count']);
	}
	
	function getdata_matkul(){
		$kd_kurikulum = $this->input->post('kd_kurikulum');
		$kd_prodi = $this->input->post('kd_prodi');
		if($kd_kurikulum != ''){
			$data = $this->mdl_khs->getmatakuliah($kd_kurikulum, $kd_prodi);
			echo $this->mdl_khs->togrid($data['row_data'], $data['row_count']);	
		}else{echo json_encode(array('total'=>0, 'rows'=>array('kd_matakuliah'=>'')));}
	}
	
	function getsemester_mhs($nim){
		$res = $this->mdl_khs->getsemester_mhs($nim);
		echo json_encode(array('semester'=>$res));
	}
	
	function getdata_mhs(){
		$mhs = $this->mdl_mahasiswa->getdataByNIM($this->input->post('nim'));
		
		if($mhs->num_rows()>0){
			$data['nama'] = $mhs->row()->nama;
			$data['nama_prodi'] = $mhs->row()->nama_prodi;
			echo json_encode($data);
		}else{
			echo json_encode(array('error'=>true));
		}
	}
	
	function laporan_pdf($kd_fakultas,$kd_prodi,$tahun,$nim){
		 $this->load->library('HTML2PDF');
		 $html2pdf = new HTML2PDF('P', 'A4', 'fr');
		 $html2pdf->setDefaultFont('Arial');
		
		//filter
		#get filter
		$fil['kd_fakultas'] = $kd_fakultas;
		$fil['kd_prodi'] = $kd_prodi;
		$fil['tahun'] = $tahun;
		$fil['nim'] = $nim;
		
		 //$data['nama'] = 'indriyanto';
		 $data['namaUniv'] = 'UNIVERSITAS OTTOW GEISSLER';
		 $data['alamatUniv'] = 'Jln. Perkutut Kotaraja, Telp.(0967)581562';
		 $data['kotaUniv'] = 'Jayapura, Papua';
		 // ambil data dari tabel
		$data['data_pdf'] = $this->mdl_khs->get_pdf($fil,$nama_dosen);
		
		/* if (count($da['row'])==0){
			echo "Data Tidak Tersedia";
			return;
		} */
		
		
		 $konten = $this->load->view('khs_laporan',$data,true);
		
         $html2pdf->writeHTML($konten, false);
        
		 $html2pdf->Output('exemple00.pdf');
	}
	
	function pdf($kd_fakultas,$kd_prodi,$tahun,$nim){
		# inisialisasi library
		$this->load->library('our_pdf');
		$this->our_pdf->FPDF('L', 'mm', 'A4');
		define('FPDF_FONTPATH',APPPATH."libraries/fpdf/font/");
		
		#get filter
		$fil['kd_fakultas'] = $kd_fakultas;
		$fil['kd_prodi'] = $kd_prodi;
		$fil['tahun'] = $tahun;
		$fil['nim'] = $nim;
		$nama_dosen ='';
		
		
		// ambil data dari tabel
		$pdfdata = $this->mdl_khs->get_pdf($fil,$nama_dosen);
		
		if (count($pdfdata['row'])==0){
			echo "Data Tidak Tersedia";
			return;
		}

		
		# create pdf
		$this->our_pdf->Open();
		$this->our_pdf->addPage();
		
		#LOGO HEADER
		$this->our_pdf->Image('asset/images/logo.png',10,3);
		//$this->our_pdf->Cell(110,9,'UNIVERSITAS OTTOW GEISSLER PAPUA',0,0,'C');
		
		// judul laporan
		$this->our_pdf->setFont('arial','B',13);
		$posY = 35;
		$posX = 10;
		$midX = 150;
		$title = 'Kartu Hasil Studi';
		$namas = ($fil['nim']!='')?$this->mdl_khs->getNameByNim($fil['nim']):'';
		//$dosens = ($fil['kd']!='')?$this->mdl_khs->getNameByNim($row['nim']):'';
		$fakultass = ($fil['kd_fakultas']!='')?$this->mdl_khs->getFakultasByKode($fil['kd_fakultas']):'';
		$prodis = ($fil['kd_prodi']!='')?$this->mdl_khs->getProdiByKode($fil['kd_prodi']):'';
		$textPosX = $midX - ($this->our_pdf->GetStringWidth($title) / 2);
		$this->our_pdf->text($textPosX,$posY,$title);
		$this->our_pdf->setFont('arial','B',10);
		$posY += 6;
		$nims = 'NIM                   : '.$nim;
		$textPosX = $midX - ($this->our_pdf->GetStringWidth($nims) / 2);
		$this->our_pdf->text($posX,$posY,$nims);
		$posY += 6;
		$namas = 'Nama                : '.$namas;
		$textPosX = $midX - ($this->our_pdf->GetStringWidth($namas) / 2);
		$this->our_pdf->text($posX,$posY,$namas);
		$posY += 6;
		$fakultass = 'Fakultas	           : '.$fakultass;
		$textPosX = $midX - ($this->our_pdf->GetStringWidth($fakultass) / 2);
		$this->our_pdf->text($posX,$posY,$fakultass);
		$posY += 6;
		$prodis = 'Program Studi	 : '.$prodis;
		$textPosX = $midX - ($this->our_pdf->GetStringWidth($prodis) / 2);
		$this->our_pdf->text($posX,$posY,$prodis);
		$posY += 6;
		$tahuns = 'Tahun Ajaran	   : '.$tahun;
		$textPosX = $midX - ($this->our_pdf->GetStringWidth($tahuns) / 2);
		$this->our_pdf->text($posX,$posY,$tahuns);
		$posY += 6;
		$dosens = 'Dosen Wali    	   : '.$nama_dosen;
		$textPosX = $midX - ($this->our_pdf->GetStringWidth($dosens) / 2);
		$this->our_pdf->text($posX,$posY,$dosens);
		
		# setting coloumn
		$this->our_pdf->setFont('Arial','B',9);
		$this->our_pdf->setFillColor(255,255,255);
		$posY += 6;
		$this->our_pdf->setXY($posX,$posY);
		
		// init column
		$col_width = array();
		$col_align = array();
		$col_caption = array();
		$col_val_align = array();
		foreach($pdfdata['column'] as $col){
			array_push($col_width, 		$col['width']);
			array_push($col_align, 		$col['align']);
			array_push($col_caption, 	$col['caption']);
			array_push($col_val_align, 	$col['val_align']);
		}
		
		// set column option
		$this->our_pdf->SetWidths($col_width);
		$this->our_pdf->SetAligns($col_align);
		$this->our_pdf->Row($col_caption);
		
		# setting row
		$posY = 39;
		$this->our_pdf->setFont('arial','',9);	
		$this->our_pdf->setFillColor(255,255,255);
		
		$this->our_pdf->SetAligns($col_val_align);
		
		// insert record
		foreach($pdfdata['row'] as $row){
			$this->our_pdf->Row($row);
		}
		
		# setting TTD
		$this->our_pdf->setFont('arial','B',13);
		$posY = 170;
		$posX = 60;
		$midX = 250;
		$title = 'Dekan, ';
		$textPosX = $midX - ($this->our_pdf->GetStringWidth($title));
		$this->our_pdf->text($textPosX,$posY,$title);
		$this->our_pdf->setFont('arial','B',13);
		$posY = 190;
		$posX = 60;
		$midX = 248;
		$title = '...........';
		$textPosX = $midX - ($this->our_pdf->GetStringWidth($title));
		$this->our_pdf->text($textPosX,$posY,$title);
		
		// finish
		$this->our_pdf->AliasNbPages();
		$this->our_pdf->Output("Data KHS.pdf", "I");
	}
}